﻿// See https://aka.ms/new-console-template for more information

//多播委托，把更多的委托包装到委托中来，形成方法的调用链
//按照顺序依次添加进去
Action action = new Action(DoSomething1);
action += DoSomething1;
action += DoSomething1;
action += DoSomething1;
action += DoSomething1;

//-= 把方法依次移除
action -= DoSomething1;
action -= DoSomething1;
action -= DoSomething1;
action -= DoSomething1;

//还可以把静态方法，实例方法，lamda表达式添加入多播委托
action += Test.DoSomething3Static;//静态方法

Test test = new Test();
action += new Test().DoSomething实例;//实例方法
action += () =>
{
    Console.WriteLine("DoSomethingLamda");
};//lamda表达式

var obj1 = () =>
{
    Console.WriteLine("DoSomethingLamda2");
};

action += obj1;

action.Invoke();

action -= DoSomething1;
action -= Test.DoSomething3Static;
action -= new Test().DoSomething实例;//重新实例化了对线，内存地址不同了，删除无效
action -= () =>
{
    Console.WriteLine("DoSomethingLamda");
};//删除无效，理由同上

action -= obj1;//删除有效，是同一个lamda表达式

action.Invoke();



Console.WriteLine("Hello, World!");

void DoSomething1()
{
    Console.WriteLine("Something1");
}

void DoSomething2()
{
    Console.WriteLine("Something2");
}

public class Test
{
    public static void DoSomething3Static()
    {
        Console.WriteLine("SomethingStatic");
    }

    public void DoSomething实例()
    {
        Console.WriteLine("DoSomething实例");
    }
}